/*********************************************************************************
Organization 					: 				Georgia Institute of Technology
  
Institute 						:				Cognitive Computing Group(CCL)
 
Authors							: 				Santiago Ontanon, Kinshuk Mishra
 												Neha Sugandh 
 												
Class							:				GetResourcesGoal
 
Function						: 				Provides a class for holding the 
												get resources goal
****************************************************************************/

package planninglanguage.goals;

import org.jdom.Element;

import planninglanguage.conditions.AndCondition;
import planninglanguage.conditions.Condition;
import planninglanguage.conditions.GoldCondition;
import planninglanguage.conditions.OilCondition;
import planninglanguage.conditions.WoodCondition;
import planninglanguage.softconditions.AndSoftCondition;
import planninglanguage.softconditions.RelativeGoldSoftCondition;
import planninglanguage.softconditions.RelativeOilSoftCondition;
import planninglanguage.softconditions.RelativeWoodSoftCondition;
import planninglanguage.softconditions.SoftCondition;
import util.XMLWriter;
import base.WargusGameState;

public class RelativeGetResourcesGoal extends Goal implements Cloneable{
	
	int m_gold, m_original_gold;
	int m_oil, m_original_oil;
	int m_wood, m_original_wood;
	int m_playerID;
	
	public RelativeGetResourcesGoal(int a_playerID,int a_gold,int a_oil,int a_wood)
	{
		m_playerID = a_playerID;
		m_gold = a_gold;
		m_oil = a_oil;
		m_wood = a_wood;
		m_original_gold = 0;
		m_original_oil = 0;
		m_original_wood = 0;
	}
	public Object clone()
	{
		return new RelativeGetResourcesGoal(m_gold,m_oil,m_wood,m_playerID);
	}
	
	public Condition generateSuccessCondition(WargusGameState a_gamestate)
	{
		m_original_gold = a_gamestate.getPlayer().getGold();
		m_original_oil = a_gamestate.getPlayer().getOil();
		m_original_wood = a_gamestate.getPlayer().getWood();
		
		return new AndCondition(new AndCondition(new GoldCondition(m_gold+m_original_gold),
												 new OilCondition(m_oil+m_original_oil)),
												 new WoodCondition(m_wood+m_original_wood));
	}
	
	public SoftCondition generateSuccessSoftCondition(WargusGameState a_gamestate)
	{
		m_original_gold = a_gamestate.getPlayer().getGold();
		m_original_oil = a_gamestate.getPlayer().getOil();
		m_original_wood = a_gamestate.getPlayer().getWood();
		
		return new AndSoftCondition(new AndSoftCondition(new RelativeGoldSoftCondition(m_gold,m_original_gold),
												 		 new RelativeOilSoftCondition(m_oil,m_original_oil)),
												 		 new RelativeWoodSoftCondition(m_wood,m_original_wood));
	}	
	
	public float distance(Goal a_g) {
		if (!(a_g instanceof RelativeGetResourcesGoal)) return 1;

		RelativeGetResourcesGoal g = (RelativeGetResourcesGoal)a_g;
		float d_g = 1;
		float d_o = 1;
		float d_w = 1;
		
		if (m_gold == g.m_gold) d_g = 0;
		if (m_oil == g.m_oil) d_o = 0;
		if (m_wood == g.m_wood) d_w = 0;
		
		return d_g*0.166f + d_o*0.166f + d_w*0.166f;	/* values are between 0 and 0.5 since at least the goal type is the same */ 
	}	
	
	public void writeXML(XMLWriter w) {
		w.tag(Goal.openTag + " " + Goal.XMLtype + "=\"" + 
				this.getClass().getSimpleName() + "\"");
		{
			w.tag(Goal.XMLplayerID, m_playerID);
			w.tag("gold", m_gold);
			w.tag("oil", m_oil);
			w.tag("wood", m_wood);
		}
		w.tag(Goal.closeTag);
	}
	protected static RelativeGetResourcesGoal loadFromXML(Element e) {
		int playerID = Integer.parseInt(e.getChildText(Goal.XMLplayerID));
		int gold = Integer.parseInt(e.getChildText("gold"));
		int oil = Integer.parseInt(e.getChildText("oil"));
		int wood = Integer.parseInt(e.getChildText("wood"));
		return new RelativeGetResourcesGoal(playerID, gold, oil, wood);
	}
}
